//
//  LJFNewHotViewController.m
//  GiftThat
//
//  Created by 刘景峰 on 16/1/8.
//  Copyright © 2016年 ChuXiang. All rights reserved.
//

#import "LJFNewHotViewController.h"
#import "TFHpple.h"
#import "UMSocial.h"
@interface LJFNewHotViewController ()<UIWebViewDelegate,UMSocialUIDelegate>
@property (nonatomic,strong) UIImageView *imageView;

@end

@implementation LJFNewHotViewController

-(void)viewWillAppear:(BOOL)animated{
    if (self.imageView) {
        [self.imageView stopAnimating];
        [self.imageView removeFromSuperview];
    }else{
        self.imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0,0 , 80, 80)];
        self.imageView.backgroundColor = [UIColor blackColor];
        self.imageView.alpha = 0.5;
        self.imageView.center = CGPointMake(self.view.width / 2, self.view.height / 2 - 80);
        NSMutableArray*muArray = [NSMutableArray array];
        for (int i = 0; i < 15; i++) {
            [muArray addObject:[UIImage imageNamed:[NSString stringWithFormat:@"loading_%d",i]]];
        }
        [self.imageView setAnimationImages:muArray];
        [self.imageView setAnimationDuration:3];
        [self.imageView startAnimating];
        [self.view addSubview:self.imageView];

    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    self.view.backgroundColor = [UIColor whiteColor];
    [self.navigationItem.leftBarButtonItem setTintColor:[UIColor whiteColor]];

    UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"share"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(shareURLString)];
    
    self.navigationItem.rightBarButtonItem = item;
    
    self.navigationItem.title = @"礼物详情";
    [self creatWebView];

    
    
    
}

-(void)webViewDidFinishLoad:(UIWebView *)webView{
    if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"Color"] isEqualToString:@"BlackColor"]) {
        
        // 字体颜色
        [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextFillColor= 'white'"];
        // 背景色
        [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.background='#2E2E2E'"];
        
    }else{
        
        
        // 字体颜色
        [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextFillColor= 'black'"];
        // 背景色
        [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.background='#FFFAFA'"];
    
    
    }

}

-(void)shareURLString{
    
    [UMSocialSnsService presentSnsIconSheetView:self
                                         appKey:@"507fcab25270157b37000010"
                                      shareText:self.urlString                                     shareImage:[UIImage imageNamed:@"icon.png"]
                                shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToWechatSession,UMShareToQQ,UMShareToTencent,nil]
                                       delegate:self];
    
}
-(BOOL)isDirectShareInIconActionSheet{
    return YES;
}

-(void)creatWebView{
    UIWebView *webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height - 113)];
    webView.delegate = self;
    webView.scalesPageToFit = YES;
    
    [CXNetWorkTool getWithUrl:self.urlString parameter:nil httpHeader:nil responseType:ResponseTypeDATA success:^(id result) {
        NSString *str = [[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding];
        //    解析html数据
        TFHpple *xpathParser = [[TFHpple alloc]initWithHTMLData:result];
        //    根据标签进行过滤
        NSArray *dataArray = [xpathParser searchWithXPathQuery:@"//div"];

        for (TFHppleElement *hppleElement in dataArray) {
            if ([hppleElement.attributes[@"class"] isEqualToString:@"wrapper download-banner"]) {
                //            根据节点属性获取广告，在用如下方法置换为空
                str = [str stringByReplacingOccurrencesOfString:hppleElement.raw withString:@""];
                
            }
        }
    [webView loadHTMLString:str baseURL:[NSURL URLWithString:self.urlString]];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self.imageView stopAnimating];
            [self.imageView removeFromSuperview];
            
        });
        
    } fail:^(NSError *error) {
        NSLog(@"%@",error);
    }];
    

    [self.view addSubview:webView];
    

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
